package com.springboot.project.common.controller;

import com.springboot.config.annotion.BusinessLog;
import com.springboot.config.exception.BussinessException;
import com.springboot.config.tool.PageUtils;
import com.springboot.config.tool.Query;
import com.springboot.config.tool.Tip;
import com.springboot.project.common.dao.DeptDao;
import com.springboot.project.common.entity.Dept;
import com.springboot.project.common.entity.Role;
import com.springboot.project.common.node.ZTreeNode;
import com.springboot.project.common.service.impl.DeptServiceImpl;
import com.springboot.project.common.service.impl.RoleServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门 前端控制器
 * </p>
 *
 * @author lby
 * @since 2018-05-06
 */
@Controller
@RequestMapping("/dept")
public class DeptController {

    private static String PREFIX = "system/dept/";

    @Autowired
    DeptDao deptDao;

    @Autowired
    private DeptServiceImpl deptService;
    /**
     * 跳转到查看 部门表列表的页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "dept";
    }

    /**
     * 加载表格
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public List<Map<String, Object>> list(@RequestParam Map<String, Object> params){
        //查询列表数据
        List<Map<String, Object>> deptList = deptService.list(params);
        return deptList;
    }

    /**
     * 跳转到添加页面
     */
    @RequestMapping("/add")
    @RequiresPermissions("/dept/add")
    public String addView() {
        return PREFIX + "dept_add";
    }


    /**
     * 跳转到修改页面
     */
    @RequestMapping("/edit/{id}")
    public String userEdit(@PathVariable Integer id, Model model) {
        Dept dept = deptService.selectById(id);
        Dept pDept = deptService.selectById(dept.getPid());
        String deptName = pDept.getSimplename();
        model.addAttribute("dept",dept);
        model.addAttribute("deptName",deptName);
        return PREFIX + "dept_edit";
    }

    /**
     * 删除 部门表指定数据
     */
    @RequestMapping("/remove/{id}")
    @BusinessLog(value = "删除部门")
    @RequiresPermissions("/dept/del")
    @ResponseBody
    public Tip remove(@PathVariable Integer id) {
        deptService.deleteById(id);
        return new Tip();
    }

    /**
     * 添加部门表
     */
    @RequestMapping("/addDept")
    @BusinessLog(value = "添加部门表")
    @RequiresPermissions("/dept/add")
    @ResponseBody
    public Tip addDept(Dept dept) {
        try {
            Dept pDept = deptService.selectById(dept.getPid());
            String pids = pDept.getPids() + "[" + pDept.getId() + "],";
            dept.setNum(10);
            dept.setPids(pids);
            deptService.insert(dept);
        }catch (Exception ex)
        {
            throw new BussinessException(500,"添加部门——添加失败！");
        }
        return new Tip();
    }

    /**
     * 修改部门表
     */
    @RequestMapping("/editDept")
    @BusinessLog(value = "修改部门表")
    @RequiresPermissions("/dept/edit")
    @ResponseBody
    public Tip editDept(Dept dept) {
        deptService.updateById(dept);
        return new Tip();
    }

    /**
     * 获取部门树
     */
    @RequestMapping("/deptTree")
    @ResponseBody
    public List<ZTreeNode> deptTree() {
        List<ZTreeNode> tree = deptDao.deptTree();
        tree.add(ZTreeNode.createParent());
        return tree;
    }
}
